package state.md5;

import state.util.array.ByteArray;
import state.util.array.IntArray;

/**
 * Fast implementation of RSA's MD5 hash generator in Java JDK Beta-2 or higher<br>
 * Originally written by Santeri Paavolainen, Helsinki Finland 1996 <br>
 * (c) Santeri Paavolainen, Helsinki Finland 1996 <br>
 * Some changes Copyright (c) 2002 Timothy W Macinta <br>
 * <p>
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * <p>
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
 * <p>
 * You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * <p>
 * See http://www.twmacinta.com/myjava/fast_md5.php for more information on this file.
 * <p>
 * Contains internal state of the MD5 class
 * <p>
 * Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util package only because it came without a package. I was not the the original author of the code, although I did optimize it (substantially) and fix some bugs.
 * 
 * @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi>
 * @author Timothy W Macinta (twm@alum.mit.edu) (optimizations and bug fixes)
 **/

class MD5State {
  /**
   * 128-bit state
   */
  IntArray state = null;

  /**
   * 64-bit character count
   */
  int count = 0;

  /**
   * 64-byte buffer (512 bits) for storing to-be-hashed characters
   */
  ByteArray buffer = null;

  public MD5State() {
    buffer = ByteArray.create(64);
    count = 0;
    state = IntArray.create(4);

    state.set(0, 0x67452301);
    state.set(1, 0xefcdab89);
    state.set(2, 0x98badcfe);
    state.set(3, 0x10325476);
  }

  public static MD5State createMD5State(MD5State from) {
    MD5State md5State = new MD5State();
    int i;
    for (i = 0; i < md5State.buffer.len(); i++) {
      md5State.buffer.set(i, from.buffer.get(i));
    }
    for (i = 0; i < md5State.state.len(); i++) {
      md5State.state.set(i, from.state.get(i));
    }
    md5State.count = from.count;
    return md5State;
  }
}
